home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 March / macformat-022.iso / Shareware City / Developers / src / out-of-phase-102-c / OutOfPhase 1.02 Source / OutOfPhase Folder / FastModulation.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-11-23  |  3.7 KB  |  81 lines  |  [TEXT/KAHL]

  1. /* FastModulation.c */
  2. /*****************************************************************************/
  3. /*                                                                           */
  4. /*    Out Of Phase:  Digital Music Synthesis on General Purpose Computers    */
  5. /*    Copyright (C) 1994  Thomas R. Lawrence                                 */
  6. /*                                                                           */
  7. /*    This program is free software; you can redistribute it and/or modify   */
  8. /*    it under the terms of the GNU General Public License as published by   */
  9. /*    the Free Software Foundation; either version 2 of the License, or      */
  10. /*    (at your option) any later version.                                    */
  11. /*                                                                           */
  12. /*    This program is distributed in the hope that it will be useful,        */
  13. /*    but WITHOUT ANY WARRANTY; without even the implied warranty of         */
  14. /*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          */
  15. /*    GNU General Public License for more details.                           */
  16. /*                                                                           */
  17. /*    You should have received a copy of the GNU General Public License      */
  18. /*    along with this program; if not, write to the Free Software            */
  19. /*    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.              */
  20. /*                                                                           */
  21. /*    Thomas R. Lawrence can be reached at tomlaw@world.std.com.             */
  22. /*                                                                           */
  23. /*****************************************************************************/
  24.  
  25. #include "MiscInfo.h"
  26. #include "Audit.h"
  27. #include "Debug.h"
  28. #include "Definitions.h"
  29.  
  30. #include "FastModulation.h"
  31. #include "Memory.h"
  32.  
  33.  
  34. /* apply a modulation chain to a value.  all vectors are of length NumModulators */
  35. /* except for OldValueVector whose length is the number of oscillators.  each */
  36. /* oscillator is looked up as OldValueVector[IndirectionTable[i]] */
  37. float                                    ApplyModulation(float Value, ModulationTypes* ModulateHow,
  38.                                                 float* ModulationScaling, float* ModulationOrigin,
  39.                                                 float* OldValueVector, long* IndirectionTable,
  40.                                                 long NumModulators)
  41.     {
  42.         long                                Scan;
  43.  
  44.         for (Scan = 0; Scan < NumModulators; Scan += 1)
  45.             {
  46.                 float                                Modulus;
  47.  
  48.                 PRNGCHK(ModulateHow,&(ModulateHow[Scan]),sizeof(ModulateHow[Scan]));
  49.                 PRNGCHK(ModulationScaling,&(ModulationScaling[Scan]),
  50.                     sizeof(ModulationScaling[Scan]));
  51.                 PRNGCHK(ModulationOrigin,&(ModulationOrigin[Scan]),
  52.                     sizeof(ModulationOrigin[Scan]));
  53.                 PRNGCHK(IndirectionTable,&(IndirectionTable[Scan]),
  54.                     sizeof(IndirectionTable[Scan]));
  55.                 PRNGCHK(OldValueVector,&(OldValueVector[IndirectionTable[Scan]]),
  56.                     sizeof(OldValueVector[IndirectionTable[Scan]]));
  57.                 Modulus = OldValueVector[IndirectionTable[Scan]]
  58.                     * ModulationScaling[Scan] + ModulationOrigin[Scan];
  59.                 switch (ModulateHow[Scan])
  60.                     {
  61.                         default:
  62.                             EXECUTE(PRERR(ForceAbort,"ApplyModulation:  bad modulation type"));
  63.                             break;
  64.                         case eModulationAdditive:
  65.                             /* y = x + (m * scale + const) */
  66.                             Value = Value + Modulus;
  67.                             break;
  68.                         case eModulationMultiplicative:
  69.                             /* y = x * (m * scaling + const) */
  70.                             /* this can be transformed into */
  71.                             /* y = x * (1 - m * scaling + const) */
  72.                             /* by doing this:  const' = 1 + const, scaling' = - scaling */
  73.                             /* y = x * (m * (-scaling) + (1 + const)) */
  74.                             /* y = x * (m * scaling' + const') */
  75.                             Value = Value * Modulus;
  76.                             break;
  77.                     }
  78.             }
  79.         return Value;
  80.     }
  81.